package com.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;

import com.aop.SystemLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.entity.Student;
import com.service.StudentService;

@Controller
@RequestMapping 
//controller 配置Request mapping 使得放的热Requestmapping变短
public class StudentController extends BaseController{
	@Autowired
	private StudentService studentService;

	
	public StudentService getStudentService() {
		return studentService;
	}

	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
		System.out.println("setter被调用");
	}
	@RequiresPermissions("student:getAll")
	@RequestMapping("student/getAll")
    public ModelAndView  getAllStudent(HttpServletRequest request) throws ClassNotFoundException, SQLException{
		List<Student> students = studentService.getAllStudent();
    	return new ModelAndView("student/list","students",students);
    }
	@RequestMapping("student/testMyTag")
    public ModelAndView  getMyTag(HttpServletRequest request) throws ClassNotFoundException, SQLException{
		String msg = "ok"; 
		request.setAttribute("msg", msg);
    	return new ModelAndView("student/test");
    }
	@RequiresRoles("student")
	@RequestMapping("student/getAllJSon")
	@SystemLog(methods = "用户管理",module = "用户登陆")
	@ResponseBody
    public List<Student>  getAllStudentJson() throws SQLException, ClassNotFoundException{
//		System.out.println("student get");
//		List<Student> students = new ArrayList<Student>();
//		students.add(new Student("0001","张三"));
//		students.add(new Student("0002","李四"));
		List<Student> students = studentService.getAllStudent();
    	return students;
    }
	@RequestMapping("student/getStudentByName/{name}")
	@ResponseBody
    public List<Student>  getStudentByName(@PathVariable("name")String name) throws SQLException, ClassNotFoundException{
		System.out.println(name);
		List<Student> students = studentService.getStudentByName(name);
    	return students;
    }
	
	@RequestMapping("student/add")
	@ResponseBody
	public boolean addStudent(Student student) throws IllegalArgumentException, IllegalAccessException, SQLException{
		System.out.println(student.getName()+" "+student.getSno()+"cccc");
		boolean result = studentService.addStudent(student);
    	return result;
    }
	@RequestMapping("student/update")
	public boolean updateStudent(Student student) throws IllegalArgumentException, IllegalAccessException, SQLException{
		System.out.println(student.getName());
		boolean result  = studentService.updateStudent(student);
    	return result;
    }

	@RequestMapping(value = "student/select/{methodname}")
	@ResponseBody
	public List<Map> select(@PathVariable("methodname")String methodname){
		return studentService.select(methodname);
	}

}
